package xm.dao;

import java.io.Reader;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import xm.vo.BoardVO;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class BoardDAO {
	private static SqlMapClient sql;
	
	static{
		try {
			Reader rd = Resources.getResourceAsReader("SqlMapConfig.xml");
			sql = SqlMapClientBuilder.buildSqlMapClient(rd);
		} catch (Exception e) {
			System.out.println("SqlMapClient error : xm.dao.BoardDAO.SqlMapConfig");
			e.printStackTrace();
		}
	}
	//dual seq
	public static int getSeq(){
		int temp = 0;
		try {
			temp= (Integer)sql.queryForObject("dualSeq");
		} catch (Exception e) {System.out.println("dual error : xm.dao.BoardDAO.getSeq");e.printStackTrace();	}
		return temp;
	}
	
	//insert
	public static void insert(BoardVO vo){
		try {
			sql.insert("boardInsert",vo);
		} catch (Exception e) {System.out.println("insert error : xm.dao.BoardDAO.insert");e.printStackTrace();}
	}
	
	//replay update
	public static void updatePnum(int pnum){
		try {
				sql.update("updatePnum", pnum);
		} catch (Exception e) {System.out.println("replay error : xm.dao.BoardDAO.updatePnum"); e.printStackTrace();	}
	}
	//
	public static void updatePnumRE(int pnum){
		try {
		sql.update("updateDelPnum", pnum);
		} catch (Exception e) {System.out.println("replay error : xm.dao.BoardDAO.updatePnumRE"); e.printStackTrace();	}
	}
	//step update
	public static void updateStep(Map<String, Object> map){
		try {
			sql.update("updateStep", map);
		} catch (Exception e) {System.out.println("step error : xm.dao.BoardDAO.updateStep"); e.printStackTrace();	}
	}
	
	//totalRow
	public static int getTotalRow(Map<String, Object> map) {
		int temp = 0;
		try {
			temp = (Integer)sql.queryForObject("totalRow", map);
		} catch (Exception e) {System.out.println("total error : xm.dao.BoardDAO.getTotalRow");e.printStackTrace();	}
		return temp;
	}
	
	//board_list
	public static List<BoardVO> getList(Map<String, Object> map) {
		List<BoardVO> list = null;
		try {
			list = sql.queryForList("boardList", map);	
		} catch (Exception e) {System.out.println("list error : xm.dao.BoardDAO.getList");e.printStackTrace();}
		return list;
	}
	
	//hit
	public static void upHit(int no) {
		try {
			sql.update("hitUp",no);
		} catch (Exception e) {System.out.println("hit error : xm.dao.BoardDAO.upHit");e.printStackTrace();}
	}
	
	//content
	public static BoardVO getContent(int no) {
		BoardVO vo = null;
		try {
			vo = (BoardVO)sql.queryForObject("content", no);
		} catch (Exception e) {System.out.println("read error : xm.dao.BoardDAO.getContent");e.printStackTrace();}
		return vo;
	}
	
	//pass
	public static Boolean getPass(Map<String, Object> map){
		boolean value = false;
		String temp = null;
		try {
			temp = (String)sql.queryForObject("searchPass", map);
			if(temp != null) value = true;
		} catch (Exception e) {System.out.println("pass error : xm.dao.BoardDAO.getPass"); e.printStackTrace();}
		return value;
	}
	
	//modfiy
	public static void modfiy(Map<String, Object> map){
		try {
			sql.update("boardModfiy", map);
		} catch (Exception e) {System.out.println("modify error : xm.dao.BoardDAO.modfiy"); e.printStackTrace();}
	}
	
	//delete
	public static void getDelete(int no){
		try {
			sql.delete("boardDel", no);
			sql.delete("cmtAllDel", no);
		} catch (Exception e) {System.out.println("delete error : xm.dao.BoardDAO.getDelete");e.printStackTrace();}
	}
	
	public static void fileInsert(Map<String, Object> map){
		try {
			sql.insert("fileInsert", map);
		} catch (SQLException e) {	e.printStackTrace();}
	}
	
}
